package com.feather.common.data.simple;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.feather.common.data.DataColumn;
import com.feather.common.data.DataRow;

/**
 * @author flogyin
 */
public class SimpleDataRow implements DataRow, java.io.Serializable {
	private static final long		serialVersionUID	= 1L;

	protected ArrayList<DataColumn>	collection			= new ArrayList<DataColumn>();
	protected boolean				throwNotFoundFlag;

	@Override
	public int getCount() {
		return this.collection.size();
	}

	public void add(DataColumn dataColumn) {
		if (dataColumn != null)
			this.collection.add(dataColumn);
	}

	@Override
	public DataColumn get(int columnIndex) {
		if (this.throwNotFoundFlag && (columnIndex < 1 || columnIndex >= this.collection.size())) {
			throw new RuntimeException("Not found: " + columnIndex + " in " + this.collection.size());
		}
		return this.collection.get(columnIndex);
	}

	@Override
	public DataColumn get(String columnName) {
		int columnIndex = indexOf(columnName);
		DataColumn dc = columnIndex > -1 ? this.collection.get(columnIndex) : null;
		if (dc == null && this.throwNotFoundFlag) {
			throw new RuntimeException("Not found: " + columnName);
		}
		return dc;
	}

	@Override
	public void set(int columnIndex, DataColumn dataColumn) {
		this.collection.set(columnIndex, dataColumn);
	}

	@Override
	public void set(int columnIndex, Object value) {
		DataColumn dataColumn = get(columnIndex);
		dataColumn.setObject(value);
	}

	@Override
	public void set(String columnName, DataColumn dataColumn) {
		if (dataColumn != null) {
			int columnIndex = indexOf(columnName);
			if (columnIndex > -1)
				this.collection.set(columnIndex, dataColumn);
		}
	}

	@Override
	public void set(String columnName, Object value) {
		DataColumn dataColumn = get(columnName);
		dataColumn.setObject(value);
	}

	@Override
	public int indexOf(String columnName) {
		if (columnName != null) {
			int size = this.collection.size();
			for (int i = 0; i < size; i++) {
				DataColumn dataColumn = this.collection.get(i);
				if (columnName.equalsIgnoreCase(dataColumn.getColumn().getName()))
					return i;
			}
		}
		return -1;
	}

	// public int indexOf(DataColumn dataColumn) {
	// return dataColumn != null ? indexOf(dataColumn.getColumn().getName()) :
	// -1;
	// }

	/*
	 * @Override public boolean isThrowNotFound() { return
	 * this.throwNotFoundFlag; }
	 * 
	 * @Override public void setThrowNotFound(boolean flag) {
	 * this.throwNotFoundFlag = flag; }
	 */

	@Override
	public Map<String, String> toMap() {
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		int colCount = getCount();
		for (int c = 0; c < colCount; c++) {
			DataColumn dataColumn = get(c);
			String colName = dataColumn.getColumn().getName();
			String lColName = colName.toLowerCase();
			String value = dataColumn.asString();
			map.put((colName.equals(lColName) || colName.equals(colName.toUpperCase())) ? lColName : colName, value);
		}
		return map;
	}

	@Override
	public String toJson() {
		return JSON.toJSONString(toMap());
	}
}
